1 package edu.jiangxin.apktoolbox.convert.color.colorspace; 2 3 import java.awt.color.ColorSpace; 4 5 public class CmykColorSpace extends ColorSpace { 6 7 protected CmykColorSpace(int type, int numComponents) { 8 super(type, numComponents); 9 } 10 11 public static CmykColorSpace getInstance() { 12 return Holder.INSTANCE; 13 } 14 15 @Override 16 public float[] toRGB(float[] colorvalue) { 17 float c = colorvalue[0]; 18 float m = colorvalue[1]; 19 float y = colorvalue[2]; 20 float k = colorvalue[3]; 21 22 float r = 1 - Math.min(1, c * (1 - k) + k); 23 float g = 1 - Math.min(1, m * (1 - k) + k); 24 float b = 1 - Math.min(1, y * (1 - k) + k); 25 26 return new float[]{r, g, b}; 27 } 28 29 @Override 30 public float[] fromRGB(float[] rgbvalue) { 31 float r = rgbvalue[0]; 32 float g = rgbvalue[1]; 33 float b = rgbvalue[2]; 34 35 float k = 1 - Math.max(r, Math.max(g, b)); 36 float c = (1 - r - k) / (1 - k); 37 float m = (1 - g - k) / (1 - k); 38 float y = (1 - b - k) / (1 - k); 39 40 return new float[]{c, m, y, k}; 41 } 42 43 @Override 44 public float[] toCIEXYZ(float[] colorvalue) { 45 float[] rgb = toRGB(colorvalue); 46 return ColorSpace.getInstance(CS_sRGB).toCIEXYZ(rgb); 47 } 48 49 @Override 50 public float[] fromCIEXYZ(float[] colorvalue) { 51 float[] rgb = ColorSpace.getInstance(CS_sRGB).fromCIEXYZ(colorvalue); 52 return fromRGB(rgb); 53 } 54 55 private static class Holder { 56 private static final CmykColorSpace INSTANCE = new CmykColorSpace(TYPE_CMYK, 4); 57 } 58 }